using System;
using System.Collections.Generic;

using Blog.Net.Common.DataObjects;
using Blog.Net.Business.BusinessObjects;

using NHibernate;
using NHibernate.Criterion;
using CureConsulting.DataAccess;

namespace Blog.Net.DataAccess.DataControls
{
	public partial class BlogEntryDataControl : IDataControl<BlogEntryBO>
	{
		#region FieldNames
		
		/// <summary>
        /// Struct to hold field names
        /// </summary>
		public struct FieldNames
        {
			/// <summary>
        	/// HQL Property Name for Id
        	/// </summary>
			public const string Id = "Id";
			/// <summary>
        	/// HQL Property Name for Subject
        	/// </summary>
			public const string Subject = "Subject";
			/// <summary>
        	/// HQL Property Name for Subhead
        	/// </summary>
			public const string Subhead = "Subhead";
			/// <summary>
        	/// HQL Property Name for Body
        	/// </summary>
			public const string Body = "Body";
			/// <summary>
        	/// HQL Property Name for Views
        	/// </summary>
			public const string Views = "Views";
			/// <summary>
        	/// HQL Property Name for Points
        	/// </summary>
			public const string Points = "Points";
			/// <summary>
        	/// HQL Property Name for Votes
        	/// </summary>
			public const string Votes = "Votes";
			/// <summary>
        	/// HQL Property Name for CreateDate
        	/// </summary>
			public const string CreateDate = "CreateDate";
			/// <summary>
        	/// HQL Property Name for BlogResponses
        	/// </summary>
			public const string BlogResponses = "BlogResponses";
			/// <summary>
        	/// HQL Property Name for BlogCategories
        	/// </summary>
			public const string BlogCategories = "BlogCategories";
		}
		
		#endregion
		
		#region Private Members
		
		private static BlogEntryDataControl blogEntryDataControl;
		private static object lockBlogEntryDataControl = new object();
		private static NHibernateDataControl<BlogEntry> dataControl = new NHibernateDataControl<BlogEntry>();
		
		#endregion
		
		#region Constructors
		
		/// <summary>
		/// Constructor for blog_entry.
		/// </summary>
		public BlogEntryDataControl(){}
		
		#endregion
		
		#region Private Methods

        private static IList<BlogEntryBO> ConvertPOCOListToBOList(IList<BlogEntry> blogEntries)
        {
            List<BlogEntryBO> retVal = new List<BlogEntryBO>();

            if (blogEntries != null)
            {
                foreach (BlogEntry blogEntry in blogEntries)
                {
                    retVal.Add(new BlogEntryBO(blogEntry));
                }
            }

            return retVal;
        }

        #endregion
		
		#region Public Properties

        /// <summary>
        ///Singleton Instance
        /// </summary>
        public static BlogEntryDataControl Instance
        {
            get
            {
                lock (lockBlogEntryDataControl)
                {
                    if(blogEntryDataControl == null)
                    {
                        blogEntryDataControl = new BlogEntryDataControl();
                    }
                }

                return blogEntryDataControl;
            }
        }

        #endregion
		
		#region CRUD

        /// <summary>
        /// Function to Save a BlogEntry
        /// </summary>
        /// <param name="blogEntry"></param>
        /// <returns></returns>
        public int? Save(BlogEntryBO blogEntry)
        {
            int? retVal = dataControl.Save(blogEntry.POCO);
            if (retVal.HasValue)
            	dataControl.Refresh(blogEntry.POCO);
            return retVal;
        }

        /// <summary>
        /// Function to Save a list of blog_entries
        /// </summary>
        /// <param name="blogEntries"></param>
        /// <returns></returns>
        public bool Save(IList<BlogEntryBO> blogEntries)
        {
            List<BlogEntry> itemsToSave = new List<BlogEntry>();

            foreach (BlogEntryBO blogEntry in blogEntries)
            {
                itemsToSave.Add(blogEntry.POCO);
            }

            return dataControl.Save(itemsToSave);
        }

        /// <summary>
        /// Function to delete a BlogEntry
        /// </summary>
        /// <param name="blogEntry"></param>
        /// <returns></returns>
        public bool Delete(BlogEntryBO blogEntry)
        {
            return dataControl.Delete(blogEntry.POCO);
        }

        /// <summary>
        /// Function to Delete a list of blog_entries
        /// </summary>
        /// <param name="blogEntries"></param>
        /// <returns></returns>
        public bool Delete(IList<BlogEntryBO> blogEntries)
        {
            List<BlogEntry> itemsToDelete = new List<BlogEntry>();

            foreach (BlogEntryBO blogEntry in blogEntries)
            {
                itemsToDelete.Add(blogEntry.POCO);
            }

            return dataControl.Delete(itemsToDelete);
        }		
		
        #endregion
		
		#region Queries
		
		/// <summary>
        /// Get BlogEntry by ID
        /// </summary>
        /// <param name="id">ID of BlogEntry</param>
        /// <returns>The BlogEntry</returns>
        public BlogEntryBO GetById(int? id)
        {
			BlogEntry retVal = dataControl.GetObjectById(id);
			
			if(retVal != null)
				return new BlogEntryBO(retVal);
			
			return null;
		}
		
		/// <summary>
        /// Return list of all blog_entries
        /// </summary>
        /// <returns></returns>
        public IList<BlogEntryBO> GetAll()
        {
			IList<BlogEntry> retVal = dataControl.GetQueryResult(dataControl.GetCriteria());

            if (retVal != null)
                return ConvertPOCOListToBOList(retVal);
         
			return new List<BlogEntryBO>();
        }
		
		/// <summary>
        /// Return list of all blog_entries
        /// </summary>
        /// <param name="firstResult"></param>
        /// <param name="maxResults"></param>
        /// <returns></returns>
        public IList<BlogEntryBO> GetAll(int firstResult, int maxResults)
        {
			IList<BlogEntry> retVal = dataControl.GetQueryResult(dataControl.GetCriteria(), firstResult, maxResults);

            if (retVal != null)
                return ConvertPOCOListToBOList(retVal);
            
			return new List<BlogEntryBO>();
        }
		
		/// <summary>
        /// Return list of all blog_entries
        /// </summary>
        /// <param name="firstResult"></param>
        /// <param name="maxResults"></param>
		/// <param name="sortExpression"></param>
        /// <returns></returns>
        public IList<BlogEntryBO> GetAll(int firstResult, int maxResults, string sortExpression)
        {
			ICriteria criteria = dataControl.GetCriteria();

            if (!String.IsNullOrEmpty(sortExpression))
            {
                string[] sort = sortExpression.Split(' ');
                bool ascending = true;
                if (sort.Length > 1 && sort[1].ToUpper() == "DESC") { ascending = false; }
                criteria.AddOrder(new NHibernate.Criterion.Order(sort[0],ascending));
            }
			
			IList<BlogEntry> retVal = dataControl.GetQueryResult(criteria, firstResult, maxResults);

            if (retVal != null)
                return ConvertPOCOListToBOList(retVal);
        
			return new List<BlogEntryBO>();
        }
		
		/// <summary>
        /// Return count of all blog_entries
        /// </summary>
        /// <returns></returns>
        public int GetCountOfAll()
        {			
			int result = (int)dataControl.GetCriteria().SetProjection(Projections.RowCount()).UniqueResult();
			return result;
        }
		
		#endregion
	}
}